package cn.dxj1016.lesson04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlDown {
    public static void main(String[] args) throws Exception {
//        1.下载地址
        URL url = new URL("https://m701.music.126.net/20210206235426/f6bc58c1b65effb7813bf84270df3e18/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/7282034514/6e0d/a7a8/e100/c9c961fb1c06d54f4ef3b6b164ec5098.m4a");

//        2.连接到这个资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream("f.m4a");

        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, len);//写出这个数组
        }
        fileOutputStream.close();
        inputStream.close();
        urlConnection.disconnect();//断开连接

    }
}
